//
//  ALoadingHeader.swift
//  NanChangRER
//
//  Created by Groot on 2017/10/17.
//  Copyright © 2017年 Groot. All rights reserved.
//

import MJRefresh

class ALoadingHeader: MJRefreshHeader {

    var loadingView: ARefreshLoadingView?
    
    fileprivate func layoutLoadingView() {
        let width = self.mj_w
        let height: CGFloat = self.mj_h
        
        let loadingViewSize: CGFloat = ARefreshConstants.LoadingViewSize
        let minOriginY = height - ARefreshConstants.LoadingViewSize - 8
        
        loadingView?.frame = CGRect(x: (width - loadingViewSize) / 2.0, y: minOriginY, width: loadingViewSize, height: loadingViewSize)

    }
    override func prepare() {
        super.prepare()
        
        self.mj_h = 70;
        
        loadingView = ARefreshLoadingViewCircle()
        self.addSubview(loadingView!)
    }
    
    override func placeSubviews() {
        super.placeSubviews()
        layoutLoadingView()
    }
    
    override var state: MJRefreshState {
        get {
            return super.state
        }
        set {
            if state == newValue {
                return
            }
            super.state = newValue
            
            switch newValue {
            case .idle:
                loadingView?.stopLoading()
                break
            case .pulling:
                break
            case .refreshing:
                loadingView?.startAnimating()
                break
            default:
                break
            }
        }
    }
    
    override var pullingPercent: CGFloat {
        set {
            super.pullingPercent = newValue
            if newValue != 0.0, newValue != 1.0 {
                loadingView?.setPullProgress(newValue)
            }
        }
        get {
            return super.pullingPercent
        }
    }
    
}
